var pmysql = require("./pmysql");
var pconfig = require("./pconfig");
var plog=require("./plog");
var plib=require("./plib");

//class ==============================
var PTable = function() {};

//成员变量 ===========================
PTable.prototype.tabname = "";
PTable.prototype.fieldsdef = {};
PTable.prototype.fields = null;

//方法 ==============================
PTable.prototype.query = function(sqlstr, cb)
{
	var con = pconfig.GetCon();
	sqlstr = sqlstr.replace("[*]", con.mysqlprefix);
	
	pmysql.GetCon().query(sqlstr, 
		function(err, rows, fields)
		{
			cb(err,rows,fields);
		}
	);
}

PTable.prototype.TabName = function()
{
	return "[*]" + this.tabname;
}
PTable.prototype.construct = function()
{
	var fields = new Array();
	fields["ID"] = 0;
	for(var key in this.fieldsdef)
	{
		var t = this.fieldsdef[key].substr(0, 3);
		switch(t)
		{
			case "INT":
				fields[key] = 0;
				break;
			case "CHR":
				fields[key] = "";
				break;
			case "TXT":
				fields[key] = "";
				break;
		}
	}	
	this.fields  = fields;
}
PTable.prototype.ReadRow = function(row)
{
	this.fields = {};
	this.fields["ID"] = row["ID"];
	for(var key in this.fieldsdef)
	{
		var t = this.fieldsdef[key].substr(0, 3);
		switch(t)
		{
			case "INT":
				this.fields[key] = row[key];
				break;
			case "CHR":
				this.fields[key] = row[key];
				break;
			case "TXT":
				this.fields[key] = row[key];
				break;
		}
	}
}

PTable.prototype.Update = function(cb)
{
	var sql_str = "";
	if(this.fields["ID"] == 0)
	{
		sql_str = this.GetInsertSQL();
		var th = this;	
		this.query(sql_str, function(err, rows, fields){
			plog.out("@@---------------------->>>> " + plib.obj2str(rows)) ;
				th.fields["ID"] = rows.insertId;
				plog.out("insert id is " + rows.insertId);
				if(cb!= null) cb(th);
			});
	} else {
		sql_str = this.GetUpdateSQL( );
		var th = this;	
		this.query(sql_str, function(err, row, fields) {
				if(cb!= null) cb(th);
			});
	}
}
PTable.prototype.GetUpdateSQL = function(cb)
{
        var sqlstr = " UPDATE `" + this.TabName() + "` SET ";
        var i = 0;
	for (var key in this.fields)
	{
		if(key != "ID")
		{
			if(i != 0) sqlstr += ","; 
			i ++;
			sqlstr += ("`" + key +"` = '" + this.fields[key] + "'");
		}
	}
	sqlstr += ( "WHERE `ID`=" + this.fields["ID"]);
	return sqlstr;
}

PTable.prototype.GetInsertSQL= function()
{
	var sqlstr = " INSERT INTO `" + this.TabName() + '` (';
	i=0;
	for(var key in this.fields)
	{
		if(key != "ID")
		{
		if(i != 0) 
				sqlstr += " , " ; 
			i ++;
			sqlstr += (" `" + key + "` ");
		}
	}
	
	sqlstr += " ) VALUES (" ;
	i=0;
	for (var  key in this.fields)
	{
		if(key != "ID")
		{
			if(i != 0) 
				sqlstr += " , " ; 
			i ++;
			sqlstr += (" '" + this.fields[key]  + "' ");
		}
	}
	sqlstr += " );" ;
	return sqlstr;
}



module.exports = PTable;

//静态方法 ==========================================
module.exports.CreateByID = function (_class, _id, _cb){
 	var one = new _class();
        var sql_str = "SELECT * FROM `" + one.TabName() + "` WHERE `ID`=" + _id + " LIMIT 0, 1";
	one.query(sql_str, function(err, rows, fields)
	{
		//plog.out(rows);
		if(rows == null) { _cb(null); return; };
		if(rows.length == 0) 
		{
			if(_cb != null) _cb(null);
		} else {
			one.ReadRow(rows[0]);	
			if(_cb != null) _cb(one);
		}
	});
}

module.exports.DeleteByID = function(_class, _id, _cb)
{
	var one = new _class();
	var sql_str = "DELETE FROM `" + one.TabName() + "` WHERE `ID` = " + _id + "";

	one.query(sql_str, function(err, rows, fields){
		_cb(0);
	});
}


module.exports.SetCreateAll = function(_class, _cb) {
	var one = new _class();
	var sql_str = "SELECT * FROM `" + one.TabName() + "`"; 
	one.query(sql_str, function(err, rows, fields)
	{
		var i = 0; 
		var o = null;
		var ret= {};
		for(var i = 0; i < rows.length; i ++)
		{
			o= new _class();
			o.ReadRow(rows[i]);
			ret[i]= o;
		}
		if(_cb != null) _cb(ret);
	});
}
